package com.example.peibei.ui.fragment;

import android.graphics.Typeface;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.dingtao.common.bean.User;
import com.dingtao.common.core.WDFragment;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.bean.CityBean;
import com.example.peibei.ui.activity.CheckInActivity;
import com.example.peibei.ui.activity.LoginActivity;
import com.example.peibei.ui.activity.MemberActivity;
import com.example.peibei.ui.adapter.MyPagerAdapter;
import com.flyco.tablayout.SlidingTabLayout;
import com.zaaach.citypicker.CityPicker;
import com.zaaach.citypicker.adapter.OnPickListener;
import com.zaaach.citypicker.model.City;
import com.zaaach.citypicker.model.HotCity;
import com.zaaach.citypicker.model.LocateState;
import com.zaaach.citypicker.model.LocatedCity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeFragmentThree extends WDFragment {
    @BindView(R.id.slideTablaout)
    SlidingTabLayout slideTablaout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.iv_activity)
    ImageView iv_activity;
    @BindView(R.id.fake_status_bar)
    View fake_status_bar;

    private String[] mTitles = {"关注", "推荐", "同城"};
    private HomeChildFragment2 homeChildFragmentThree;
    private SPUtils spUtils;
    TextView title3;

    @Override
    public String getPageName() {
        return "首页Fragment";
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_three;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        spUtils = new SPUtils(getActivity(), SpConstant.USER_TOKEN);
//        spUtils.putString(SpConstant.CITY_NAME, "北京");
//        spUtils.putString(SpConstant.CITY_CODE, "101010100");
//        EventBus.getDefault().register(this);
        HomeChildFollowFragment homeChildFragmentOne = new HomeChildFollowFragment().setType("2");
        HomeChildFragment2 homeChildFragmentTwo = new HomeChildFragment2().setType("1");
        homeChildFragmentThree = new HomeChildFragment2().setType("0");
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(homeChildFragmentOne);
        fragments.add(homeChildFragmentTwo);
        fragments.add(homeChildFragmentThree);
        MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager(), fragments, mTitles);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(3);
        slideTablaout.setViewPager(viewpager);
        viewpager.setCurrentItem(1);
        TextView title = slideTablaout.getTitleView(1);
        title.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        title.setTextSize(18);
        title3 = slideTablaout.getTitleView(2);
        String cityName = spUtils.getString(SpConstant.CITY_NAME);
        if (TextUtils.isEmpty(cityName)){
            title3.setText("同城");
        }else{
            String substring = cityName.substring(0, cityName.length() - 1);
            title3.setText(substring);
        }

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                int tabCount = slideTablaout.getTabCount();
                for (int i = 0; i < tabCount; i++) {
                    TextView title = slideTablaout.getTitleView(i);
                    if (i == position) {
                        title.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                        title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
                    } else {
                        title.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                        title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(CityBean cityBean) {
        String cityName = cityBean.getCityName();
        TextView titleView = slideTablaout.getTitleView(2);
        if (TextUtils.isEmpty(cityName)){
            titleView.setText("同城");
            return;
        }
        String substring = cityName.substring(0, cityName.length() - 1);
        titleView.setText(substring);
    }

    @OnClick({R.id.tv_sign_in, R.id.iv_activity})
    public void signIn() {
        User loginUser = SpConstant.getLoginUser();
        if (loginUser.getId() == 0){
            intent(LoginActivity.class);
            return;
        }
        intent(CheckInActivity.class);
    }

    @OnClick({R.id.iv_address})
    public void changeCity() {
        boolean isVip = spUtils.getBoolean(SpConstant.VIP, false);
        if (isVip){
            getLocatedCity();
        }else{
            intent(MemberActivity.class);
        }
    }

    private void getLocatedCity() {
        List<HotCity> hotCities = new ArrayList<>();
        hotCities.add(new HotCity("北京", "北京", "101010100"));
        hotCities.add(new HotCity("上海", "上海", "101020100"));
        hotCities.add(new HotCity("广州", "广东", "101280101"));
        hotCities.add(new HotCity("深圳", "广东", "101280601"));
        hotCities.add(new HotCity("杭州", "浙江", "101210101"));
        CityPicker.getInstance()
                .setFragmentManager(getActivity().getSupportFragmentManager())  //此方法必须调用
                .enableAnimation(true)  //启用动画效果
//                .setAnimationStyle(anim)  //自定义动画
                .setLocatedCity(new LocatedCity("北京", "北京", "101010100"))  //APP自身已定位的城市，默认为null（定位失败）
                .setHotCities(hotCities)  //指定热门城市
                .setOnPickListener(new OnPickListener() {
                    @Override
                    public void onPick(int position, City data) {
                        if (data != null) {
                            String cityName = data.getName();
//                            String substring = cityName.substring(0, cityName.length()-1);
                            title3.setText(cityName);
                            Log.i("answer", "当前城市：" + cityName);//城市信息
                            Log.i("answer", "当前城市：" + data.getCode());//城市信息
                            homeChildFragmentThree.updateData(data.getCode(), cityName);
                            spUtils.putString(SpConstant.CITY_NAME, cityName);
                            spUtils.putString(SpConstant.CITY_CODE, data.getCode());
                        }
                    }

                    @Override
                    public void onLocate() {
                        //开始定位，这里模拟一下定位
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                //定位完成之后更新数据
                                CityPicker.getInstance()
                                        .locateComplete(new LocatedCity("深圳", "广东", "101280601"), LocateState.SUCCESS);
                            }
                        }, 2000);
                    }
                })
                .show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
